import 'dart:typed_data';

import 'package:dio/dio.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_juwo/common/common_index.dart';
import 'package:flutter_juwo/ui/custom/LoadingDialog.dart';
import 'package:flutter_juwo/ui/custom/MySeparator.dart';
import 'package:flutter_juwo/ui/custom/real_rich_text.dart';
import 'package:flutter_juwo/ui/utils/toast_utils.dart';
import 'package:flutter_juwo/ui/utils/utils.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';

class ServiceDialog extends Dialog {
  final String startTime;
  final String endTime;
  final String wx;
  final String imgUrl;
  final bool isHome;

  ServiceDialog(this.startTime, this.endTime, this.wx, this.imgUrl,
      {this.isHome = true});

  @override
  Widget build(BuildContext context) {
    return new Material(
      //创建透明层
      type: MaterialType.transparency, //透明类型
      child: new Center(
        //保证控件居中效果
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            SizedBox(
              width: ScreenUtil().setWidth(600),
              child: Column(
                children: <Widget>[
                  Container(
                    decoration: ShapeDecoration(
                      color: Color(0xffffffff),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(8.0)),
                      ),
                    ),
                    child: new Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        ///文本
                        Container(
                          width: ScreenUtil().setWidth(750),
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(6),
                              color: Color(0xFFFBF4E6)),
                          margin: EdgeInsets.fromLTRB(
                              ScreenUtil().setWidth(10),
                              ScreenUtil().setWidth(20),
                              ScreenUtil().setWidth(10),
                              ScreenUtil().setWidth(10)),
                          padding: EdgeInsets.only(
                              bottom: ScreenUtil().setWidth(20)),
                          child: Column(
                            children: <Widget>[
                              Container(
                                margin: EdgeInsets.fromLTRB(
                                    ScreenUtil().setWidth(15),
                                    ScreenUtil().setWidth(20),
                                    0,
                                    0),
                                alignment: Alignment.centerLeft,
                                child: Text(
                                  "唯一官方客服微信：${wx}",
                                  style: TextStyles.custom(30, Colours.appC1,
                                      Font.PingFangBlod),
                                ),
                              ),
                              isHome
                                  ? Container(
                                      margin: EdgeInsets.fromLTRB(
                                          ScreenUtil().setWidth(15),
                                          ScreenUtil().setWidth(20),
                                          0,
                                          0),
                                      alignment: Alignment.centerLeft,
                                      child: RealRichText([
                                        TextSpan(
                                            text: "有问题先看",
                                            style: TextStyles.custom(
                                                26,
                                                Colours.appC2,
                                                Font.PingFangRegular)),
                                        TextSpan(
                                            text: "【常见问题】",
                                            style: TextStyles.custom(
                                                28,
                                                Colours.appC11,
                                                Font.PingFangMedium),
                                            recognizer: TapGestureRecognizer()
                                              ..onTap = () {
                                                Application.router.navigateTo(
                                                    context,
                                                    Routes.commonProblem);
                                              }),
                                        TextSpan(
                                            text: "找不到答案再问客服",
                                            style: TextStyles.custom(
                                                26,
                                                Colours.appC2,
                                                Font.PingFangRegular)),
                                      ]),
                                    )
                                  : Container(),
                              Container(
                                alignment: Alignment.centerLeft,
                                margin: EdgeInsets.only(
                                    left: ScreenUtil().setWidth(16)),
                                child: Text(
                                  "客服工作时间：$startTime-$endTime",
                                  style: TextStyles.custom(26, Colours.appC3,
                                      Font.PingFangRegular),
                                ),
                              )
                            ],
                          ),
                        ),

                        ///图片
                        Container(
                          alignment: Alignment.center,
                          margin:
                              EdgeInsets.only(top: ScreenUtil().setWidth(30)),
                          child: GestureDetector(
                            onTap: () {
                              print("点击了图片");
                              showDialog(
                                  context: context,
                                  barrierDismissible: false,
                                  builder: (BuildContext context) {
                                    return new LoadingDialog(
                                      text: "保存中…",
                                    );
                                  });
                              _saved(imgUrl, context);
                            },
                            child: UiUtils.cachedNetworkImage(260, 260, imgUrl),
                          ),
                        ),
                        Container(
                          margin:
                              EdgeInsets.only(top: ScreenUtil().setWidth(20)),
                          child: Row(
                            children: <Widget>[
                              Container(
                                height: 30,
                                width: 15,
                                decoration: BoxDecoration(
                                    color: Colors.black54,
                                    borderRadius: BorderRadius.only(
                                        bottomRight: Radius.circular(50),
                                        topRight: Radius.circular(50))),
                              ),
                              Expanded(
                                flex: 1,
                                child: Container(
                                  height: 30,
                                  child: MySeparator(color: Colours.c243),
                                ),
                              ),
                              Container(
                                height: 30,
                                width: 15,
                                decoration: BoxDecoration(
                                    color: Colors.black54,
                                    borderRadius: BorderRadius.only(
                                        bottomLeft: Radius.circular(50),
                                        topLeft: Radius.circular(50))),
                              ),
                            ],
                          ),
                        ),
                        Container(
                          alignment: Alignment.center,
                          child: GestureDetector(
                            onTap: () {
                              ClipboardData data =
                                  new ClipboardData(text: "${wx}");
                              Clipboard.setData(data);
                              ToastUtil.showShortToast("已复制");
                            },
                            child: Text(
                              "复制微信号",
                              style: TextStyles.custom(34, Colours.appC11,
                                  Font.PingFangMedium),
                            ),
                          ),
                        ),
                        Container(
                          margin: EdgeInsets.only(
                              bottom: ScreenUtil().setWidth(40)),
                          alignment: Alignment.center,
                          child: Text(
                            "点击图片可保存到本地相册",
                            style: TextStyles.custom(30, Colours.appC2,
                                Font.PingFangMedium),
                          ),
                        )
                      ],
                    ),
                  ),
                ],
              ),
            ),
            Container(
              child: GestureDetector(
                child: Image.asset(
                  Utils.getImgPath("icon_cancle"),
                  height: ScreenUtil().setWidth(80),
                  width: ScreenUtil().setWidth(80),
                ),
                onTap: () {
                  Application.router.pop(context);
                },
              ),
            )
          ],
        ),
      ),
    );
  }

  _saved(String imgUrl, BuildContext context) async {
    var response = await Dio()
        .get(imgUrl, options: Options(responseType: ResponseType.bytes));
    final result =
        await ImageGallerySaver.save(Uint8List.fromList(response.data));
    Navigator.pop(context);
    if (result) {
      ToastUtil.showShortToast("保持成功,请在相册中查看");
    } else {
      ToastUtil.showShortErrToast("保持失败，请您自行截图保存");
    }
  }
}
